﻿using System;
using System.Collections.Generic;
using System.Linq;
using DataAccess.Interface;
using Locator.LcboServices.LcboStoresService.Interface.Services;
using Locator.LcboServices.PageLoaders.Services;
using Locator.LcboServices.LcboStoresService.Services;
using DataAccess;
using System.Diagnostics;
using DataAccess.BusinessEntities;
using Locator.LcboServices.BusinessEntities;

namespace StoreScanner
{
    class Program
    {
        private static IUnitOfWork unitOfWork;
        private static ILcboStoresService lcboStoresService;

        private static void Main(string[] args)
        {
            Stopwatch getProductsStopwatch = new Stopwatch();
            getProductsStopwatch.Start();

            //lcboStoresService = new LcboStoresServiceAgent(new TestPageRetrieverFactory());
            lcboStoresService = new LcboStoresServiceAgent(new LivePageRetrieverFactory());
            unitOfWork = new SqlUnitOfWork();
            var storesInDb = unitOfWork.Stores.FindAll();
            Dictionary<int, Store> dbStoreDictionary = new Dictionary<int, Store>();
            int storesInDbCount = 0;
            foreach (Store store in storesInDb)
            {
                storesInDbCount++;
                dbStoreDictionary.Add(store.Id, store);
            }

            Console.WriteLine(String.Format("Found {0} stores in the DB", storesInDbCount));

            var lcboStores = lcboStoresService.GetAllStores();

            Dictionary<int, LcboStoreInformation> lcboStoresDictionary = new Dictionary<int, LcboStoreInformation>();

            List<int> lcboStoreIds = new List<int>();
            foreach (LcboStoreInformation lcboStore in lcboStores)
            {
                lcboStoresDictionary.Add(lcboStore.StoreNumber, lcboStore);
                lcboStoreIds.Add(lcboStore.StoreNumber);
            }

            lcboStoreIds.Sort();
            //Console.WriteLine("LCBO ctore numbers are:");
            //foreach (int lcboStoreId in lcboStoreIds)
            //{
            //    Console.WriteLine(lcboStoreId);
            //}

            // Figure out changed stores and update them in the unitOfWork
            int newStoreCount = 0;
            foreach (int lcboStoreId in lcboStoresDictionary.Keys)
            {
                LcboStoreInformation lcboStore = lcboStoresDictionary[lcboStoreId];
                if (dbStoreDictionary.ContainsKey(lcboStoreId))
                {
                    Store dbStore = dbStoreDictionary[lcboStoreId];
                    dbStore.Address = lcboStore.AddressLine;
                    dbStore.City = lcboStore.City;
                    dbStore.Latitude = lcboStore.Latitude;
                    dbStore.Longitude = lcboStore.Longitude;
                }
                else
                {
                    Store newDbStore = new Store() { Address = lcboStore.AddressLine,
                    City = lcboStore.City,
                    Id = lcboStore.StoreNumber,
                    Latitude = lcboStore.Latitude,
                    Longitude = lcboStore.Longitude };

                    newStoreCount++;
                    unitOfWork.Stores.Add(newDbStore);
                }
            }

            unitOfWork.Commit();

            getProductsStopwatch.Stop();
            Console.WriteLine(String.Format("Found {0} stores on the LCBO website", lcboStores.Count));
            Console.WriteLine(String.Format("Found {0} new stores on the LCBO website", newStoreCount));
            Console.WriteLine(String.Format("Elapsed time in seconds: {0:0.00}", getProductsStopwatch.ElapsedMilliseconds / 1000.00));
        }
    }
}
